//<![CDATA[
// Name: powerpack
// Description: provides animations + UI, font-size adjuster, navmenu
// Author: Mark Schenk
// Copyright: 2004, Opera Software

var hdir = "rtl"; // left-to-right, ltr || right-to-left, rtl || none
var vdir = "btt"; // bottom-to-top, btt || top-to-bottom, ttb (buggy) || none
var width = screen.width;
var height = screen.height;
var step = 30;
var pause = 10; 
var slidedisplay = 0;
var slides = new Array();
var hor = 0;
var ver = 0;
var basefont;
var changefont;
var ns = "http://www.w3.org/1999/xhtml";


function slideShow () {
	var divs = document.getElementsByTagName("div");
	var k = 0;
	for (var i=0; i<divs.length;i++) {
		if (divs[i].className == "slide") {
			slides[k++] = divs[i];
		}
	}
}

  
function doAnim() {
	// check if there is a slides array or not, otherwise create it
	if (!slides[0]) { slideShow(); }

	slidedisplay++;

	// check whether at end or beginning of presentation
	if (slidedisplay >= (slides.length)) { slidedisplay = (slides.length);  return;}
	else if (slidedisplay < 0) { slidedisplay = 0; return; }

	// some extra conditions for animations
	hdir == "none" ? hor = 0 : hor = width;	
	vdir == "none" ? ver = 0 : ver = height;	

	doMove();
}

// this doMove allows diagonal movements but it is buggy when coming from above     
function doMove() {
	var slide = slides[slidedisplay];
	var slidekids = slide.childNodes;
		
	if (hdir != "none"){ hor = hor - width/step; }
	if (vdir != "none") { ver = ver - height/step; }

	if ( hor>0 || ver>0) { 
		for (var j=0;j<slidekids.length;j++) {
			if (slidekids[j].nodetype = 1 && slidekids[j].style) {
			slidekids[j].style.position = "relative";
			if (hdir == "ltr") {slidekids[j].style.left = -hor + "px";}
			else if (hdir == "rtl") {slidekids[j].style.left = hor + "px";}
			if (vdir == "ttb") { slidekids[j].style.top = -ver + "px"; }
			else if (vdir == "btt") { slidekids[j].style.top = ver + "px"; }
			}
		}
	}
	else if ( hor<=0 && ver<=0) { 

	for (var j=0;j<slidekids.length;j++) {
		if (slidekids[j].nodetype = 1 && slidekids[j].style) {
		slidekids[j].style.left = "0px";
		slidekids[j].style.top = "0px";
		}
	}
	return;
	
	}
	setTimeout("doMove()",pause);
}

// navigation menu
function navMenu () {
	var titles = new Array();
	var slides = new Array();
	var divs = document.getElementsByTagName("div");

	for (var i=0, k=0; i<divs.length;i++) {
		if (divs[i].className == "slide") {
			slides[k] = divs[i];
			if (divs[i].getElementsByTagName("h1")[0]) {titles[k] = divs[i].getElementsByTagName("h1")[0];}
			else if (divs[i].getElementsByTagName("h2")[0]) {titles[k] = divs[i].getElementsByTagName("h2")[0];}
			else if (divs[i].getElementsByTagName("h3")[0]) {titles[k] = divs[i].getElementsByTagName("h3")[0];}
			else {titles[k] = ""; }

			if(!slides[k].id) { slides[k].id = "autonum" + (k+1); }
			k++;
		}
	}
	
	
	var y = document.body.appendChild(document.createElementNS(ns,'div'));
	y.id = 'navmenu';

	var a = y.appendChild(document.createElementNS(ns,'div'));
	a.appendChild(document.createTextNode('Slide navigation [close]'));
	a.onclick = 'toggleNavMenu()';

	var z = y.appendChild(document.createElementNS(ns,'ol'));
	for (var i=0; i<slides.length; i++) {
		var tmp = z.appendChild(document.createElementNS(ns,'a'));

		titles[i].firstChild ? tmp.innerHTML = titles[i].firstChild.data : tmp.innerHTML = "Slide " + (i+1);
		tmp.href = '#' + slides[i].id;
		tmp.onclick = 'slidedisplay =' + i + '; toggleNavMenu();';
	}
	
	document.getElementById('navmenu').style.display = "none";
}

function controlMenu () {
	basefont = window.getComputedStyle(document.getElementsByTagName('body')[0],null).fontSize;
	changefont = basefont.match(/^(\d+)px$/)[1];

	var y = document.body.appendChild(document.createElementNS(ns,'div'));
	y.id = 'controlmenu';
	y.style.fontSize = window.getComputedStyle(y,null).fontSize;

	var a = y.appendChild(document.createElementNS(ns,'div'));
	a.appendChild(document.createTextNode('Control menu [close]'));
	a.onclick = 'toggleControlMenu()';

	var dl = y.appendChild(document.createElementNS(ns,'dl'));

	// font-size adjuster
	var dt = dl.appendChild(document.createElementNS(ns,'dt'));
	dt.appendChild(document.createTextNode('Change FontSize:'));

	var dd = dl.appendChild(document.createElementNS(ns,'dd'));
	dd.appendChild(document.createTextNode('[+]'));
	dd.onclick='increaseFontSize();'

	var dd = dl.appendChild(document.createElementNS(ns,'dd'));
	dd.appendChild(document.createTextNode('[restore]'));
	dd.onclick='restoreFontSize()';

	var dd = dl.appendChild(document.createElementNS(ns,'dd'));
	dd.appendChild(document.createTextNode('[-]'));
	dd.onclick='decreaseFontSize();'

	// animation adjuster
	var dl = y.appendChild(document.createElementNS(ns,'dl'));
	dl.id = 'animset';

	// horizontal
	var dt = dl.appendChild(document.createElementNS(ns,'dt'));
	dt.appendChild(document.createTextNode('Horizontal animation direction'));

	var dd = dl.appendChild(document.createElementNS(ns,'dd'));
	dd.id = 'hltr';
	dd.appendChild(document.createTextNode('ltr'));
	dd.onclick='hdir="ltr";animdirection();'

	var dd = dl.appendChild(document.createElementNS(ns,'dd'));
	dd.id = 'hnone';
	dd.appendChild(document.createTextNode('none'));
	dd.onclick='hdir="none";animdirection();'

	var dd = dl.appendChild(document.createElementNS(ns,'dd'));
	dd.id = 'hrtl';
	dd.appendChild(document.createTextNode('rtl'));
	dd.onclick='hdir="rtl";animdirection();'

	// vertical
	var dt = dl.appendChild(document.createElementNS(ns,'dt'));
	dt.appendChild(document.createTextNode('Vertical animation direction'));

	var dd = dl.appendChild(document.createElementNS(ns,'dd'));
	dd.id = 'vbtt';
	dd.appendChild(document.createTextNode('btt'));
	dd.onclick='vdir="btt";animdirection();'

	var dd = dl.appendChild(document.createElementNS(ns,'dd'));
	dd.id = 'vnone';
	dd.appendChild(document.createTextNode('none'));
	dd.onclick='vdir="none";animdirection();'

	var dd = dl.appendChild(document.createElementNS(ns,'dd'));
	dd.id = 'vttb';
	dd.appendChild(document.createTextNode('ttb'));
	dd.onclick='vdir="ttb";animdirection();'
	
	document.getElementById('controlmenu').style.display = "none";
	animdirection();
}


function animdirection() {
	var animdirs = document.getElementById('animset').getElementsByTagName('dd');
	for (var j=0;j<animdirs.length;j++) {
		animdirs[j].style.fontWeight = 'normal';
	}
	document.getElementById('v'+vdir).style.fontWeight = 'bold';
	document.getElementById('h'+hdir).style.fontWeight = 'bold';
}


function increaseFontSize() {
	changefont = Math.round(1.2*changefont);
	document.getElementsByTagName('body')[0].style.fontSize = changefont +'px';
}

function decreaseFontSize() {
	changefont = Math.round(0.8*changefont);
	document.getElementsByTagName('body')[0].style.fontSize = changefont +'px';
}

function restoreFontSize() {
	if (document.getElementsByTagName('body')[0].hasAttribute('style')) { document.getElementsByTagName('body')[0].removeAttribute('style');}
	changefont = basefont.match(/^(\d+)px$/)[1];
}

function toggleNavMenu() {
	if (!document.getElementById('navmenu')) {navMenu();}
	if (document.getElementById('controlmenu')) { document.getElementById('controlmenu').style.display = "none"; }
	
	var navmenu = document.getElementById('navmenu');
	navmenu.style.display == "none" ? navmenu.style.display = "block" : navmenu.style.display = "none";
}

function toggleControlMenu() {
	if (!document.getElementById('controlmenu')) {controlMenu();}
	if (document.getElementById('navmenu')) { document.getElementById('navmenu').style.display = "none"; }
	
	var controlmenu = document.getElementById('controlmenu');
	controlmenu.style.display == "none" ? controlmenu.style.display = "block" : controlmenu.style.display = "none";
}

function leaveFullScreen() {
	if (document.getElementById('controlmenu')) { document.getElementById('controlmenu').style.display = "none"; }
	if (document.getElementById('navmenu')) { document.getElementById('navmenu').style.display = "none"; }
	if (document.getElementsByTagName('body')[0].hasAttribute('style')) { document.getElementsByTagName('body')[0].removeAttribute('style');}
}


// Handle keypresses
function dokey(evt) {
	var ch = evt.keyCode;

	// check to see if you're in fullscreen. Dirty hack, but hey?! :-)
	var firstslide = document.getElementById('slide1');
	if (window.getComputedStyle(firstslide,null).pageBreakAfter == "always") {
		if (ch == "34" || ch == "32" ) {  doAnim(); }
		else if (ch == "33" ) { slidedisplay = slidedisplay -2; doAnim(); }
		else if (ch == "0" ) { slidedisplay = -1; doAnim(); }
		else if (ch == "110" ) {  toggleNavMenu(); }
		else if (ch == "99" ) {  toggleControlMenu(); } // c toggles controlmenu
		else if (ch == "122" || ch == "27" ) {  leaveFullScreen(); } // F11/Esc clear up menus
	}
	else {
		if (ch == "110" ) {  toggleNavMenu(); }
	}
}
	


// Check if OperaShow file
function checkOShow() {
	var metas = document.getElementsByTagName('meta');
	for (var i=0;i<metas.length;i++) { if (metas[i].getAttribute('content').match(/^OSF.(\d+)\.(\d+)$/)) { document.addEventListener('keypress', dokey, false); return; } }
	alert('Cannot apply Script. File is not compatible Opera Show')
}

checkOShow();

//]]>
